package com.estar.integaration.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.estar.integaration.Mapper.CartGoodsMapper;
import com.estar.integaration.Service.ICartGoodsService;
import com.estar.integaration.Service.ICartService;
import com.estar.integaration.entity.CartGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YJL
 * @since 2021-11-18
 */
@Service
public class CartGoodsServiceImpl extends ServiceImpl<CartGoodsMapper, CartGoods> implements ICartGoodsService {

    @Autowired
    CartGoodsMapper cartGoodsMapper;

    @Autowired
    ICartService cartService;


    /**
     * 功能： 根据购物车id检索出对应的购买的东西
     * @param cartId  购物车id
     * @return   cartGoodsList
     */
    @Override
    public List<CartGoods> getcartGoodsBycartId(String cartId) {
        List<CartGoods> cartGoodsList = new ArrayList<>();
        QueryWrapper<CartGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cart_id",cartId);
        cartGoodsList = cartGoodsMapper.selectList(queryWrapper);
        return cartGoodsList;
    }
    /**
     * 功能： 根据cartId清空购买清单
     * @param cartId
     */
    @Override
    public void clearMyCartByCartId(String cartId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cart_id",cartId);
        cartGoodsMapper.delete(queryWrapper);
    }

    @Override
    public List<CartGoods> selectList(QueryWrapper queryWrapper) {

        return cartGoodsMapper.selectList(null);
    }

    @Override
    public HashMap<String, Double> getAllMaterialDepartNeedBuyByCartId(String userId) {
        String cardId = cartService.findCartByUserId(userId).getId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cart_id",cardId);
        List<CartGoods> cartGoodList = cartGoodsMapper.selectList(queryWrapper);//找到购物车里面属于用户的物料
        //返回物料购买情况 <物料id,个数>
        HashMap<String,Double> buyMaterialQuantity = new HashMap<>();
        for(int j = 0 ; j < cartGoodList.size(); j++){
            CartGoods cartGoods = cartGoodList.get(j);
            if(buyMaterialQuantity.containsKey(cartGoods.getMaterialId())){//如果有多个则合并
                Double oldNum = buyMaterialQuantity.get(cartGoods.getMaterialId());
                Double newNum = cartGoods.getThisBuy() == null ? 0d : cartGoods.getThisBuy() + oldNum;
                buyMaterialQuantity.put(cartGoods.getMaterialId(),newNum);
            }else {
                buyMaterialQuantity.put(cartGoods.getMaterialId(),cartGoods.getThisBuy());
            }
        }
        return buyMaterialQuantity;
    }

    @Override
    public List<CartGoods> selectCartGoodsByproductId(String productId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id",productId);
        List<CartGoods> cartGoodsList = new ArrayList<>();
        return cartGoodsList;
    }
}
